

import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import {data} from "@/hook/hook"

import {reactive, ref} from "vue"
import { ElLoading } from 'element-plus'
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Index',
    component: () => import( /* webpackChunkName: "Index" */ '../views/Index.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import( /* webpackChunkName: "Login" */ '../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import( /* webpackChunkName: "Register" */ '../views/Register.vue')
  },

  {
    path: '/admin',
    name: 'AdminLogin',
    component: () => import( /* webpackChunkName: "AdminLogin" */ '../components/admin/AdminLogin.vue'),
  },
  
  {
    path:'/adminHome',
    name:'AdminHome',
    meta:{iden:"admin"},
    component:()=>import( /* webpackChunkName: "AdminHome" */ '../views/AdminHome.vue'),
  },


  {
    path: '/search',
    name: 'SearchPage',
    component: () => import( /* webpackChunkName: "SearchPage" */ '../views/SearchPage.vue'),
  },


  {
    path: '/userhome',
    name: 'UserHome',
    meta: { iden: "user" },
    component: () => import( /* webpackChunkName: "UserHome" */ '../views/UserHome.vue'),

  
  },
  {
    path: '/enterprisehome',
    name: 'EnterpriseHome',
    meta: { iden: "enterprise" },
    component: () => import( /* webpackChunkName: "EnterpriseHome" */ '../views/EHome.vue'),
    // children:[
    //   {
    //     path:'einfo',
    //     component:()=>import( /* webpackChunkName: "Home" */ "@/components/enterprise/EnterpriseInfo.vue")
    //   }
    // ],

  },
  {
    path: '/postionInfoPage',
    name: 'PostionInfoPage',
    component: () => import( /* webpackChunkName: "PostionInfoPage" */ '../views/PostionInfoPage.vue'),

  },
  {
    path: '/eInfoPage',
    name: 'EInfoPage',
    component: () => import( /* webpackChunkName: "EInfoPage" */ '../views/EInfoPage.vue'),
  },
  {
    path: '/:catchAll(.*)',
    name: '/404',
    component: () => import( /* webpackChunkName: "/404" */ '../views/404.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

const loadInstance=ref()
router.beforeEach((to, from, next) => {
  
  // next()
  const logindata = data()
  // console.log(JSON.parse(logindata || "").token);
  if ( to.path == "/"|| to.path == "/postionInfoPage" || to.path == "/eInfoPage"|| to.path == "/search"   ) {
    
    loadInstance.value=ElLoading.service({
      lock: true,
      text: '加载中',
      background: 'rgba(255, 255, 255, 255)',
    })
    setTimeout(() => {
      next()
    }, 500);
    
    
  }
  else if(to.path=="/404"|| to.path == "/admin"||to.path == "/login" || to.path == "/register" ){
    next()
  }
  else if (logindata && logindata.token) {
    // console.log("用户");
    const identity = logindata.identity
      if (identity == to.meta.iden) {
        loadInstance.value=ElLoading.service({
          lock: true,
          text: '加载中',
          background: 'rgba(255, 255, 255, 255)',
        })
        setTimeout(() => {
          next()
        }, 500);
        // 页面加载
  
      } else {
        router.replace("/404")
      }
    
  }
  else{
    console.log("游客非法访问");
    
    router.replace("/404")
  }

  

})

router.afterEach(() => {
  if (loadInstance.value){
    setTimeout(() => {
    
      loadInstance.value.close()
    }, 800);
  }
 
})

export default router
